package com.roc.service.impl;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.roc.dao.DaoException;
import com.roc.dao.ISQLBaseDao;
import com.roc.pojo.Operators;
import com.roc.pojo.UnPay;
import com.roc.pojo.UnPayStatement;
import com.roc.service.ServiceException;
import com.roc.service.unPay.IUnPayService;
import com.roc.util.Constance;
import com.roc.web.action.unPay.UnPayStatementSearchCondition;

@Service
@Transactional
public class UnPayServiceImpl implements IUnPayService {
	static Logger logger=Logger.getLogger(UnPayServiceImpl.class);

	@Autowired
	private ISQLBaseDao sqlBaseDao;
	
	
	@Override
	public int add(UnPay unPay, Operators op) throws ServiceException {
		String sql = " INSERT INTO T_UNPAY_MERINFO(ID, MERCHANT_NO, TERMINAL_NO, FEE_T1, FEE_T1_TOP, FEE_T0, ACCOUNT_NO, ACCOUNT_NAME, BANK_NAME, BANK_CODE, YL_ID, ADD_TIME, OPER, REMARK) " +
				" VALUES (SEQ_UNPAY_MERINFO.NEXTVAL,?,?,?,?,?,?,?,?,?,?,SYSTIMESTAMP,?,?) ";
		
		int count = 0;
				
		DecimalFormat df = new DecimalFormat("#.####");
		
		String feeT1 = Constance.Null2EmptyTrim(unPay.getFeeT1());
		String feeT1Top = Constance.Null2EmptyTrim(unPay.getFeeT1Top());
		String feeT0 = Constance.Null2EmptyTrim(unPay.getFeeT0());
		feeT1 = df.format(new BigDecimal(feeT1).divide(new BigDecimal(100))); 
		feeT1Top = df.format(new BigDecimal(feeT1Top)); //.multiply(new BigDecimal(100)))
		feeT0 = df.format(new BigDecimal(feeT0).divide(new BigDecimal(100))); 
		
		List<Object> parmas=new ArrayList<Object>();
		parmas.add(Constance.Null2EmptyTrim(unPay.getMerchantNo()));
		parmas.add(Constance.Null2EmptyTrim(unPay.getTerminalNo()));
		parmas.add(feeT1);
		parmas.add(feeT1Top);
		parmas.add(feeT0);
		parmas.add(Constance.Null2EmptyTrim(unPay.getAccountNo()));
		parmas.add(Constance.Null2EmptyTrim(unPay.getAccountName()));
		parmas.add(Constance.Null2EmptyTrim(unPay.getBankName()));
		parmas.add(Constance.Null2EmptyTrim(unPay.getBankCode()));
		parmas.add(Constance.Null2EmptyTrim(unPay.getYlId()));
		parmas.add(Constance.Null2EmptyTrim(op.getAccountNo()));
		parmas.add(Constance.Null2EmptyTrim(unPay.getRemark()));
		
		try {
			count=sqlBaseDao.deleteBySql(sql, parmas);
			if(count>0)
			{
				logger.info("添加直连商户【"+unPay.getMerchantNo()+"成功】!");
			}
		} catch (DaoException e) {
			logger.debug(e.getMessage(),e);
			throw new ServiceException("添加直连商户失败");
		}
		return count;
	}


	@Override
	public UnPay update(UnPay unPay) throws ServiceException {
		
		String sql = " select t.id,t.merchant_no,t.terminal_no,t.fee_t1,t.fee_t1_top,t.fee_t0,t.account_no,t.account_name,t.bank_name,t.bank_code,t.yl_id,t.remark from T_UNPAY_MERINFO t where t.id=? ";
		Object[] parmas={unPay.getId()};
		UnPay un = null;
		try {
			Object object=sqlBaseDao.findObj(sql, parmas);
			if(object!=null){
				Object[] obj=(Object[]) object;
				un = new UnPay();
				
				DecimalFormat df = new DecimalFormat("#.##");
				
				
				String feeT1 = Constance.Null2EmptyTrim(obj[3]);
				String feeT1Top = Constance.Null2EmptyTrim(obj[4]);
				String feeT0 = Constance.Null2EmptyTrim(obj[5]);
				feeT1 = df.format(new BigDecimal(feeT1).multiply(new BigDecimal(100))); 
				feeT1Top = df.format(new BigDecimal(feeT1Top)); //.divide(new BigDecimal(100))
				feeT0 = df.format(new BigDecimal(feeT0).multiply(new BigDecimal(100))); 
				
				un.setId(Long.parseLong(Constance.Null2EmptyTrim(obj[0])));
				un.setMerchantNo(Constance.Null2EmptyTrim(obj[1]));
				un.setTerminalNo(Constance.Null2EmptyTrim(obj[2]));
				un.setFeeT1(feeT1.toString());
				un.setFeeT1Top(feeT1Top.toString());
				un.setFeeT0(feeT0.toString());
				un.setAccountNo(Constance.Null2EmptyTrim(obj[6]));
				un.setAccountName(Constance.Null2EmptyTrim(obj[7]));
				un.setBankName(Constance.Null2EmptyTrim(obj[8]));
				un.setBankCode(Constance.Null2EmptyTrim(obj[9]));
				un.setYlId(Constance.Null2EmptyTrim(obj[10]));
				un.setRemark(Constance.Null2EmptyTrim(obj[11]));
			}
			
		} catch (Exception e) {
			logger.debug(e.getMessage(),e);
			throw new ServiceException("查询【"+unPay.getMerchantNo()+"】失败");
		}
		return un;
	}


	@Override
	public int updateUnPay(UnPay unPay, Operators op) throws ServiceException {
		String sql = " UPDATE T_UNPAY_MERINFO T SET T.FEE_T1=?,T.FEE_T1_TOP=?,T.FEE_T0=?,T.ACCOUNT_NO=?,T.ACCOUNT_NAME=?,T.BANK_NAME=?,T.BANK_CODE=?,T.YL_ID=?,T.REMARK=?,T.OPER=?,T.EDIT_TIME=SYSTIMESTAMP WHERE T.ID=? ";
		int count = 0;
		List<Object> parmas=new ArrayList<Object>();
		
		DecimalFormat df = new DecimalFormat("#.####");
		
		String feeT1 = Constance.Null2EmptyTrim(unPay.getFeeT1());
		String feeT1Top = Constance.Null2EmptyTrim(unPay.getFeeT1Top());
		String feeT0 = Constance.Null2EmptyTrim(unPay.getFeeT0());
		feeT1 = df.format(new BigDecimal(feeT1).divide(new BigDecimal(100))); 
		feeT1Top = df.format(new BigDecimal(feeT1Top)); //.multiply(new BigDecimal(100))
		feeT0 = df.format(new BigDecimal(feeT0).divide(new BigDecimal(100))); 
		
		parmas.add(feeT1);
		parmas.add(feeT1Top);
		parmas.add(feeT0);
		parmas.add(Constance.Null2EmptyTrim(unPay.getAccountNo()));
		parmas.add(Constance.Null2EmptyTrim(unPay.getAccountName()));
		parmas.add(Constance.Null2EmptyTrim(unPay.getBankName()));
		parmas.add(Constance.Null2EmptyTrim(unPay.getBankCode()));
		parmas.add(Constance.Null2EmptyTrim(unPay.getYlId()));
		parmas.add(Constance.Null2EmptyTrim(unPay.getRemark()));
		parmas.add(Constance.Null2EmptyTrim(op.getAccountNo()));
		parmas.add(Constance.Null2EmptyTrim(unPay.getId()));
		
		try {
			count=sqlBaseDao.deleteBySql(sql, parmas);
			if(count>0){
				logger.info("修改银联直连商户【"+unPay.getMerchantNo()+"】成功!");		
			}
		} catch (DaoException e) {
			logger.debug(e.getMessage(),e);
			throw new ServiceException("修改银联直连商户失败~");
		}
		return count;
	}


	@Override
	public int delete(UnPay unPay) throws ServiceException {
		String sql = " DELETE FROM T_UNPAY_MERINFO T  WHERE T.ID=? ";
		
		int count = 0;
		List<Object> parmas=new ArrayList<Object>();
		
		parmas.add(Constance.Null2EmptyTrim(unPay.getId()));
		
		try {
			count=sqlBaseDao.deleteBySql(sql, parmas);
			if(count>0){
				logger.info("删除银联直连商户【"+unPay.getMerchantNo()+"】成功!");		
			}
		} catch (DaoException e) {
			logger.debug(e.getMessage(),e);
			throw new ServiceException("删除银联直连商户失败~");
		}
		return count;
	}


	@Override
	public List<UnPayStatement> queryStatement(UnPayStatementSearchCondition searchCondition) throws ServiceException {
		String sql = " SELECT * FROM (SELECT U.MERCHANTNO,U.TERMINALNO, SUM(U.AMT), SUM(U.FEE),SUM(U.SETTLEAMT),U.TRANSDATE FROM YJ_UPAY_TRANS U GROUP BY U.TRANSDATE,U.MERCHANTNO,U.TERMINALNO) tmp_table,T_UNPAY_MERINFO T "
                        +"  WHERE T.MERCHANT_NO =tmp_table.MERCHANTNO  AND T.TERMINAL_NO = tmp_table.TERMINALNO AND  tmp_table.transdate>=?  AND  tmp_table.transdate<=? ";
                          
		
		List<UnPayStatement> unPayList =  new ArrayList<UnPayStatement>();
		List<Object> objList=new ArrayList<Object>();
		Object[] parmas={searchCondition.getStartDate(),searchCondition.getEndDate()};
		
		try {
			objList = sqlBaseDao.queryAll(sql, parmas);
			
			if(null !=objList && objList.size()>0){
				Iterator iterator=objList.iterator();
				while(iterator.hasNext()){
					UnPayStatement us = new UnPayStatement();
					Object[] obj = (Object[]) iterator.next();
					
					DecimalFormat df=new DecimalFormat("#.##");//数据库存的分，转成元保留小数点后两位
					String feeT1 = Constance.Null2EmptyTrim(obj[9]);
					String feeT1Top = Constance.Null2EmptyTrim(obj[10]);
					String feeT0 = Constance.Null2EmptyTrim(obj[11]);
					feeT1 = df.format(new BigDecimal(feeT1).multiply(new BigDecimal(100))); 
					feeT1Top = df.format(new BigDecimal(feeT1Top)); //.divide(new BigDecimal(100))
					feeT0 = df.format(new BigDecimal(feeT0).multiply(new BigDecimal(100))); 
					
					us.setMerchantNo(Constance.Null2EmptyTrim(obj[0]));
					us.setTerminalNo(Constance.Null2EmptyTrim(obj[1]));
					if(null==Constance.Null2EmptyTrim(obj[2]) || "".equals(Constance.Null2EmptyTrim(obj[2]))){
						us.setSumAmt(Constance.Null2EmptyTrim(obj[2]));
					}else{
						us.setSumAmt((df.format(Double.parseDouble(Constance.Null2EmptyTrim(obj[2])))).toString());
					}
					if(null==Constance.Null2EmptyTrim(obj[3]) || "".equals(Constance.Null2EmptyTrim(obj[3]))){
						us.setSumFee(Constance.Null2EmptyTrim(obj[3]));
					}else{
						us.setSumFee((df.format(Double.parseDouble(Constance.Null2EmptyTrim(obj[3])))).toString());
					}
					if(null==Constance.Null2EmptyTrim(obj[4]) || "".equals(Constance.Null2EmptyTrim(obj[4]))){
						us.setSumSettleamt(Constance.Null2EmptyTrim(obj[4]));
					}else{
						us.setSumSettleamt((df.format(Double.parseDouble(Constance.Null2EmptyTrim(obj[4])))).toString());
					}
					us.setTransDate(Constance.Null2EmptyTrim(obj[5]));
					us.setFeeT1(feeT1.toString());
					us.setFeeT1Top(feeT1Top.toString());
					us.setFeeT0(feeT0.toString());
					us.setAccountNo(Constance.Null2EmptyTrim(obj[12]));
					us.setAccountName(Constance.Null2EmptyTrim(obj[13]));
					us.setBankName(Constance.Null2EmptyTrim(obj[14]));
					us.setBankCode(Constance.Null2EmptyTrim(obj[15]));
					us.setYlId(Constance.Null2EmptyTrim(obj[16]));
					
					unPayList.add(us);
				}
			
			}
			
		} catch (Exception e) {
			logger.debug(e.getMessage(),e);
			throw new ServiceException("查询银联直连商户报表导出失败~");
		}
		
		return unPayList;
	}
	

}
